
import { defineStore } from 'pinia'
import { ref } from 'vue'
import mapConstant from '@/utils/map'
import mapstorage from '@/utils/mapstorage'

export const mapStore = defineStore(
  'map',
  () => {
    const lotgla = ref(mapstorage.getSmall(mapConstant.lotgla) === false ? false : true)
    const maptype = ref(mapstorage.getSmall(mapConstant.maptype))
    const geodetic = ref(mapstorage.getSmall(mapConstant.geodetic) === false ? false : true)
    const position = ref(mapstorage.getSmall(mapConstant.position) === true ? true : false)



    async function initMapState() {
      maptype.value = await mapstorage.getSmall(mapConstant.maptype)
    }

    function SET_LOTGLA(val: any) {
      lotgla.value = val
      mapstorage.setSmall(mapConstant.lotgla, val)
    }

    function SET_GEODETIC(val: any) {
      geodetic.value = val
      mapstorage.setSmall(mapConstant.geodetic, val)
    }

    const SET_MAPTYPE = (val: any) => {
      maptype.value = val
      mapstorage.setSmall(mapConstant.maptype, val)
    }

    const SET_POSITION = (val: any) => {
      position.value = val
      // mapstorage.setSmall(mapConstant.position, val)
    }


    function SUM_MAPTYPE(num: number) {
      const current = Number(maptype.value);

      let newCount = current + num;

      // 防止出现负数
      if (newCount < 0) newCount = 0;

      maptype.value = newCount;

      // 存储为字符串（如果你的 storage 要求是 string）
      mapstorage.setSmall(mapConstant.maptype, String(newCount));
    }

    return {
      initMapState,
      lotgla,
      maptype,
      geodetic,
      position,
      SET_LOTGLA,
      SET_GEODETIC,
      SET_MAPTYPE,
      SUM_MAPTYPE,
      SET_POSITION,
    }
  }
)
